﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;
using System.Reflection;
using Opalis.QuickIntegrationKit;
using System.Management;
using System.Management.Automation; // Windows PowerShell namespace 
using System.Management.Automation.Runspaces; // Windows PowerShell namespac

namespace Erlacher.Opalis.VMMExtended
{
    [OpalisObject("Get Template", ShowFilters = true)]
    public class GetTemplate : IOpalisObject 
    {
        private Settings settings;
        private string s_name = string.Empty;
        private Collection<TemplateInfo> l_template = new Collection<TemplateInfo>();
        private string s_ErrorState = string.Empty;
        private string s_ErrorMessage = string.Empty;
        private string s_Trace = string.Empty;


        [OpalisConfiguration]
        public Settings Settings
        {
            set { settings = value; }
            get { return settings; }
        }

        public void Design(IOpalisDesigner designer)
        {
          
            designer.AddCorellatedData(typeof(TemplateInfo));

        }
        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {

            Dictionary<string, object> publishedData = new Dictionary<string, object>();

            string psscript = "";
            Collection<PSObject> Results;

            Assembly _assembly = Assembly.GetExecutingAssembly();
            StreamReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Erlacher.Opalis.VMMExtended.PSScripts.GetTemplate.ps1"));
            psscript = _textStreamReader.ReadToEnd();


            Results = PowerShellHelper.RunScriptReturnArray(psscript.ToString(), settings.DomainName + @"\" + settings.UserName, settings.Password, settings.VMMServer);

            
            List<TemplateInfo> infoList = VMMHelper.GetTemplateInfoList(Results);
          

            response.WithFiltering().PublishRange(infoList);

            response.Publish(publishedData);
            
             
        }


    }
}